package com.ddxz.middleground.master.service.impl;

import com.ddxz.middleground.master.dto.MasterHeader;
import com.ddxz.middleground.master.dto.role.RoleDetail;
import com.ddxz.middleground.master.dto.role.RolePage;
import com.ddxz.middleground.master.dto.role.RoleInfo;
import com.ddxz.middleground.master.entity.RoleEntity;
import com.ddxz.middleground.master.mapper.MenuPermissionMapper;
import com.ddxz.middleground.master.mapper.RoleMapper;
import com.ddxz.middleground.master.mapper.RolePermissionMapper;
import com.ddxz.middleground.master.mapper.UserRoleMapper;
import com.ddxz.middleground.master.service.RoleService;
import com.ddxz.tool.core.common.CommonUtil;
import com.ddxz.tool.core.convert.BeanUtil;
import com.ddxz.tool.core.convert.CollectionUtil;
import com.ddxz.tool.core.convert.DdxzUtil;
import com.ddxz.tool.core.convert.StringUtil;
import com.ddxz.tool.web.dto.DdxzResponse;
import com.ddxz.tool.web.dto.PageResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.ddxz.middleground.master.util.ErrorStatus.*;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/29
 */

@Service
public class RoleServiceImpl implements RoleService {
    @Override
    public DdxzResponse roleList(MasterHeader header, RolePage page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<RoleEntity> roleList = roleMapper.getList(page.getStatus(), page.getKeyword(), header.getTenant());
        return DdxzResponse.correct(PageResponse.getInstance(roleList, RoleDetail.class, DdxzUtil.SECOND_DATE));
    }

    @Override
    public DdxzResponse getRoleDetail(String roleId, MasterHeader header) {
        RoleEntity entity = roleMapper.getRoleById(roleId, header.getTenant());
        CommonUtil.ddxzAssetNull(entity, null, ROLE_NOT_EXISTS);
        return DdxzResponse.correct(BeanUtil.getTarget(entity, RoleDetail.class, DdxzUtil.SECOND_DATETIME));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DdxzResponse saveRole(RoleInfo role, MasterHeader header) {
        checkStatus(role.getStatus());
        RoleEntity entity = new RoleEntity();
        BeanUtil.copyProperties(role, entity);
        if(StringUtil.isEmpty(role.getId())) {
            roleMapper.modifyRole(entity, header.getTenant());
            return DdxzResponse.correct("保存角色成功", entity.getId());
        }

        Integer num = roleMapper.getNumByName(role.getName(), header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, ROLE_NAME_REPEAT);

        num = roleMapper.getNumByCode(role.getCode(), header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, ROLE_CODE_REPEAT);

        entity.setId(CommonUtil.generatorId(T_ROLE));
        entity.setUserId(header.getUserId());
        entity.setCreator(header.getNickname());
        roleMapper.saveRole(entity, header.getTenant());

        return DdxzResponse.correct(entity.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DdxzResponse roleRemove(MasterHeader header, String roleId) {
        Integer num = userRoleMapper.getNumByRole(roleId, header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, ROLE_HAS_USER);
        roleMapper.removeRole(roleId, header.getTenant());
        perMapper.removePerm(roleId, header.getTenant());
        return DdxzResponse.correct("删除角色成功", OK_MESSAGE);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DdxzResponse roleGrant(MasterHeader header, String roleId, List<String> permIdList) {
        if(CollectionUtil.isEmpty(permIdList)) {
            perMapper.removePerm(roleId, header.getTenant());
            return DdxzResponse.correct("授权成功", OK_MESSAGE);
        }

        perMapper.removeRolePerm(roleId, permIdList, header.getTenant());
        perMapper.grantRole(roleId, permIdList, header.getTenant());
        return DdxzResponse.correct("授权成功", OK_MESSAGE);
    }

    @Override
    public List<String> getMenuByRoleId(String roleId, String tenant) {
        List<String> perIdList = perMapper.getPermissionList(StringUtil.getList(roleId), tenant);
        return menuMapper.getMenuUrlByPermissions(perIdList, tenant);
    }

    private final RoleMapper roleMapper;

    private final MenuPermissionMapper menuMapper;

    private final RolePermissionMapper perMapper;

    private final UserRoleMapper userRoleMapper;

    public RoleServiceImpl(RoleMapper roleMapper, MenuPermissionMapper menuMapper,
                           RolePermissionMapper perMapper, UserRoleMapper userRoleMapper) {
        this.roleMapper = roleMapper;
        this.menuMapper = menuMapper;
        this.perMapper = perMapper;
        this.userRoleMapper = userRoleMapper;
    }
}
